package nc.bs.pub.action;

import nc.bs.common.projectFAUtils;
import nc.bs.framework.common.NCLocator;
import nc.bs.pub.compiler.AbstractCompiler2;
import nc.impl.pub.util.db.DBAccessUtil;
import nc.itf.pu.m23.maintain.IArriveMaintain;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.vo.pu.m23.entity.ArriveVO;
import nc.vo.pu.m23.env.ArrivalUIToBSEnv;
import nc.vo.pu.m23.pub.ArrivalEnvExtractUtil;
import nc.vo.pub.BusinessException;
import nc.vo.pub.CircularlyAccessibleValueObject;
import nc.vo.pub.compiler.PfParameterVO;
import nc.vo.pub.lang.UFDouble;
import nc.vo.uap.pf.PFBusinessException;

public class N_23_SAVEBASE extends AbstractCompiler2 {
	public String getCodeRemark() {
		return "  Object retValue = null;\n      \n      nc.vo.pu.m23.entity.ArriveVO[] inObject = (nc.vo.pu.m23.entity.ArriveVO[]) getVos();\n      nc.vo.pu.m23.env.ArrivalUIToBSEnv[] envs =\n          nc.vo.pu.m23.pub.ArrivalEnvExtractUtil.getEnvs(vo);      \n      retValue = nc.bs.framework.common.NCLocator.getInstance().lookup(nc.itf.pu.m23.maintain.IArriveMaintain.class)\n          .saveBase(inObject,envs);\n      \n      \n      return retValue;\n";
	}

	public Object runComClass(PfParameterVO vo) throws BusinessException {
		try {
			super.m_tmpVo = vo;
			Object retValue = null;
			ArriveVO[] inObject = (ArriveVO[]) ((ArriveVO[]) this.getVos());
			ArrivalUIToBSEnv[] envs = ArrivalEnvExtractUtil.getEnvs(vo);
			
			//lhw 231128 保存前校验项目 begin
			new projectFAUtils().projectDataApprove("po_arriveorder", inObject, "save");
			//end
			
//			//到货单保存校验开支代号（项目），开支代号停用则到货单不允许保存。humf
//			for(int i = 0;i<inObject.length;i++) {
//				CircularlyAccessibleValueObject[] bvos = inObject[i].getChildrenVO();
//				for(int j = 0;j<bvos.length;j++) {
//					DBAccessUtil dbutil = new DBAccessUtil();
//					String zt = dbutil.executeQuery("select pk_project from bd_project where pk_project = '"+bvos[j].getAttributeValue("cprojectid")+"' and enablestate = 2", new ColumnProcessor());
//					if(bvos[j].getAttributeValue("cprojectid") != null && (zt == null||"".equals(zt))) {
//						throw new BusinessException("项目已停用到货单不可保存");
//					}
//				}
//			}
			
			// 税率校验,货单表体行找到对应仓库，校验需要对比是否非零不是要完全相等。比如到货单税率是3%，对应仓库税率9%也要允许保存
			for (int u = 0; u < inObject.length; u++) {
				ArriveVO arrvo = inObject[u];
				CircularlyAccessibleValueObject[] bvos = arrvo.getChildrenVO();
				for (int y = 0; y < bvos.length; y++) {// 表体循环税率
					String receivestore = (String) bvos[y].getAttributeValue("pk_receivestore");// 仓库
					UFDouble ntaxrate = (UFDouble) bvos[y].getAttributeValue("ntaxrate");// 税率
					DBAccessUtil dbutil = new DBAccessUtil();
					if (receivestore != null && ntaxrate != null) {
						Object sl = dbutil.executeQuery("SELECT\n" + "tax.taxrate\n" + "FROM\n" + "bd_stordoc sto\n"
								+ "LEFT JOIN bd_taxrate tax ON\n" + "sto.def1 = tax.pk_taxcode\n" + "WHERE\n"
								+ " sto.pk_stordoc = '" + receivestore + "'\n ", new ColumnProcessor());

						if (sl != null) {
							if (ntaxrate.setScale(8, UFDouble.ROUND_HALF_UP).equals(UFDouble.ZERO_DBL)) {// 当表体税率为零
								// 仓库税率不为零则报错
								if (!(new UFDouble(sl + "").setScale(8, UFDouble.ROUND_HALF_UP)
										.equals(UFDouble.ZERO_DBL))) {
									throw new BusinessException("表体税率为零时，收货仓库关联税率必须为零");
								}
							} else {
								if (new UFDouble(sl + "").setScale(8, UFDouble.ROUND_HALF_UP)
										.equals(UFDouble.ZERO_DBL)) {
									throw new BusinessException("当表体税率不为零时，收货仓库关联税率不可为零");
								}
							}
						}
					}
				}
			}
			retValue = ((IArriveMaintain) NCLocator.getInstance().lookup(IArriveMaintain.class)).saveBase(inObject,
					envs);
			return retValue;
		} catch (Exception var5) {
			if (var5 instanceof BusinessException) {
				throw (BusinessException) var5;
			} else {
				throw new PFBusinessException(var5.getMessage(), var5);
			}
		}
	}
}